﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using Business;

namespace QuanLyNhaSach
{
    public partial class TraCuuSach : Form
    {
        public TraCuuSach()
        {
            InitializeComponent();
        }
        private void SetUpDataGridViewSach()
        {
            dgvSach.ColumnCount = 4;
            dgvSach.Columns[0].Name = "Mã Sách";
            dgvSach.Columns[1].Name = "Tên Sách";           
            dgvSach.Columns[2].Name = "Đơn Giá";
            dgvSach.Columns[3].Name = "Số Lượng Tồn";

            DataGridViewComboBoxColumn comboboxcolumnTheLoai = new DataGridViewComboBoxColumn();
           comboboxcolumnTheLoai.Name = "Thể Loại";
           comboboxcolumnTheLoai.DataSource = TheLoaiService.GetAllTheLoai();
           comboboxcolumnTheLoai.DisplayMember = TheLoaiColumns.TenTheLoai;
           comboboxcolumnTheLoai.ValueMember = TheLoaiColumns.MaTheLoai;

           DataGridViewComboBoxColumn comboboxcolumnTacGia = new DataGridViewComboBoxColumn();
           comboboxcolumnTacGia.DataSource = TacGiaService.GetAllTacGia();
           comboboxcolumnTacGia.DisplayMember = TacGiaColumns.TenTacGia;
           comboboxcolumnTacGia.ValueMember = TacGiaColumns.MaTacGia;

           DataGridViewComboBoxColumn comboboxcolumnNhaXuatBan = new DataGridViewComboBoxColumn();
           comboboxcolumnNhaXuatBan.DataSource = NhaXuatBanService.GetAllNhaXuatBan();
           comboboxcolumnNhaXuatBan.DisplayMember = NhaXuatBanColumns.TenNhaXuatBan;
           comboboxcolumnNhaXuatBan.ValueMember = NhaXuatBanColumns.MaNhaXuatBan;

           dgvSach.Columns.Insert(2, comboboxcolumnTheLoai);
           dgvSach.Columns.Insert(3, comboboxcolumnTacGia);
           dgvSach.Columns.Insert(4, comboboxcolumnNhaXuatBan);
            

        }
        private void ThemDongVaoGridView(SachDTO sachDTO)
        {
            DataGridViewRow row = new DataGridViewRow();
            row.CreateCells(dgvSach);
            row.Cells[0].Value = sachDTO.MaSach;
            row.Cells[1].Value = sachDTO.TenSach;
            row.Cells[2].Value = sachDTO.MaTheLoai;
            row.Cells[3].Value = sachDTO.MaTacGia;
            row.Cells[4].Value = sachDTO.MaNhaXuatBan;
            row.Cells[5].Value = sachDTO.DonGia;
            row.Cells[6].Value = sachDTO.SoLuongTon;
            dgvSach.Rows.Add(row);

        }
        private void LoadTheLoai()
        {
            cbTheLoai.DisplayMember = TheLoaiColumns.TenTheLoai;
            cbTheLoai.ValueMember = TheLoaiColumns.MaTheLoai;
            TheLoaiDTO theloai = new TheLoaiDTO();
            theloai.MaTheLoai = 0;
            theloai.TenTheLoai = "None";
            cbTheLoai.Items.Add(theloai);
            foreach (TheLoaiDTO theloaiDTO in TheLoaiService.GetAllTheLoai())
            {
                cbTheLoai.Items.Add(theloaiDTO);
            }
            cbTheLoai.SelectedIndex = 0;
        }
        private void LoadTacGia()
        {
            cbTacGia.DisplayMember = TacGiaColumns.TenTacGia;
            cbTacGia.ValueMember = TacGiaColumns.MaTacGia;
            TacGiaDTO tacgia = new TacGiaDTO();
            tacgia.MaTacGia = 0;
            tacgia.TenTacGia = "None";
            cbTacGia.Items.Add(tacgia);
            foreach (TacGiaDTO tacgiaDTO in TacGiaService.GetAllTacGia())
            {
                cbTacGia.Items.Add(tacgiaDTO);
            }
            cbTacGia.SelectedIndex = 0;
        }
        private void LoadNhaXuatBan()
        {
            cbNhaXuatBan.DisplayMember = NhaXuatBanColumns.TenNhaXuatBan;
            cbNhaXuatBan.ValueMember = NhaXuatBanColumns.MaNhaXuatBan;
            NhaXuatBanDTO nhaxuatban = new NhaXuatBanDTO();
            nhaxuatban.MaNhaXuatBan = 0;
            nhaxuatban.TenNhaXuatBan = "None";
            cbNhaXuatBan.Items.Add(nhaxuatban);
            foreach (NhaXuatBanDTO nhaxuatbanDTO in NhaXuatBanService.GetAllNhaXuatBan())
            {
                cbNhaXuatBan.Items.Add(nhaxuatbanDTO);
            }
            cbNhaXuatBan.SelectedIndex = 1;
          
        }
        private void TraCuuSach_Load(object sender, EventArgs e)
        {


            LoadNhaXuatBan();
            LoadTacGia();
            LoadTheLoai();
           

          
            
            SetUpDataGridViewSach();
          
      
            
           
            
            

        }

        private void txtMaSach_TextChanged(object sender, EventArgs e)
        {
            dgvSach.Rows.Clear();
            if(!string.IsNullOrEmpty(txtMaSach.Text))
            {
                int masach = int.Parse(txtMaSach.Text);
                if (SachService.KiemTraTonTaiMaSach(masach))
                {
                    SachDTO sachDTO = SachService.GetSachByID(masach);
                    ThemDongVaoGridView(sachDTO);
                }
                else
                {
                    ErrorProvider error = new ErrorProvider();
                    error.SetError(txtMaSach, "Không tồn tại mã sách");

                }
              
            
            }

        }

        private void btnTim_Click(object sender, EventArgs e)
        {



            if (!string.IsNullOrEmpty(txtTenSach.Text))
            {
                dgvSach.Rows.Clear();
                TheLoaiDTO theloai = (TheLoaiDTO)cbTheLoai.SelectedItem;
                NhaXuatBanDTO nhaxuatban = (NhaXuatBanDTO)cbNhaXuatBan.SelectedItem;
                TacGiaDTO tacgia = (TacGiaDTO)cbTacGia.SelectedItem;

                SachDTO sach = new SachDTO();
                sach.MaTheLoai = theloai.MaTheLoai;
                sach.MaTacGia = tacgia.MaTacGia;
                sach.MaNhaXuatBan = nhaxuatban.MaNhaXuatBan;
                sach.TenSach = txtTenSach.Text;
                // sach.MaSach =int.Parse( txtMaSach.Text);


                foreach (SachDTO sachDTO in SachService.GetTimSach(sach))
                {
                    ThemDongVaoGridView(sachDTO);
                }

            }
            else
            {
                dgvSach.Rows.Clear();
                TheLoaiDTO theloai = (TheLoaiDTO)cbTheLoai.SelectedItem;
                NhaXuatBanDTO nhaxuatban = (NhaXuatBanDTO)cbNhaXuatBan.SelectedItem;
                TacGiaDTO tacgia = (TacGiaDTO)cbTacGia.SelectedItem;

                SachDTO sach = new SachDTO();
                sach.MaTheLoai = theloai.MaTheLoai;
                sach.MaTacGia = tacgia.MaTacGia;
                sach.MaNhaXuatBan = nhaxuatban.MaNhaXuatBan;
                sach.TenSach = txtTenSach.Text;
                // sach.MaSach =int.Parse( txtMaSach.Text);


                foreach (SachDTO sachDTO in SachService.GetTimSach(sach))
                {
                    ThemDongVaoGridView(sachDTO);
                }
            }
        }

        
    }
}
